
var china ;

/*$(document).ready(function() {
	alert("222222222222");
	getCity("findAllCity");
	alert(china);
	BindCity('海淀区');
});*/
function getCity(action ){
	$.ajax({
		contentType : "application/x-www-form-urlencoded; charset=utf-8",
		type : "POST",// 用POST方式传输
		dataType : "json",// 数据格式:JSON
		url : action,// 目标地址
		data : "",
		async: false, 
		error : function() {
			alert("jsonerror");
		},
		success : function(json) {
			china = json;
		}
	});
}

 function BindProvince(provinceId){
    var opt0 = "省份";
    var ProvinceCount=china.length;
    var ddlProvince = document.getElementById(provinceId);
    ddlProvince.innerHTML = "";
    ddlProvince.options[0] = new Option(opt0,"");
    for(var i=0; i<ProvinceCount; i++){
        ddlProvince.options[i+1] = new Option(china[i][0],china[i][0]);
    }
 }

 function BindCity(City , provinceId , cityId ){
	var opt0 = "省份";
    var ProvinceCount=china.length;
    var ddlProvince = document.getElementById(provinceId);
    ddlProvince.innerHTML = "";
    ddlProvince.options[0] = new Option(opt0,"");
    
    var opt0City = "城市";
    var ddlCity = document.getElementById(cityId);
    ddlCity.innerHTML = "";
    ddlCity.options[0] = new Option(opt0City,"");

    var flag=false;
    var chose=true;
    var selectProvinceIndex=-1;
    for(var i=0; i<ProvinceCount; i++){
        if(!flag){
            var cityCount = china[i].length;
            for(var j=1; j<cityCount; j++){
            	
                if(china[i][j].split(",")[0]==City)
                {	
                	flag=true;
                    selectProvinceIndex=i;
                    break;
                }
            }
        }
        var arrProvince = china[i][0].split(",");
        ddlProvince.options[i+1] = new Option(arrProvince[0],arrProvince[1]);
        if(flag && chose){
            ddlProvince.options[i+1].selected = true;
            chose=false;
        }
    }
    var cityCount = 0;
    if(selectProvinceIndex!=-1){
    	 cityCount = china[selectProvinceIndex].length;
     }
    
    for(var i=0; i<cityCount; i++){
    	
    	if(i<cityCount-1){
    	var arrCity = china[selectProvinceIndex][i+1].split(",");
    	ddlCity.options[i] = new Option(arrCity[0],arrCity[1]);
   
        if(arrCity[0]==City){
            ddlCity.options[i].selected=true;
        }}
    }

 }

 function selectMoreCity(sbj , cityId){
        var opt0 = "城市";
        if(sbj.selectedIndex==0){
            var ddlCity = document.getElementById(cityId);
            ddlCity.innerHTML = "";
            ddlCity.options[0] = new Option(opt0,"");
            return;	
        }
        var selectProvince = sbj.options[sbj.selectedIndex].value;
        var ProvinceCount = china.length;
        for(var i=0; i<ProvinceCount; i++){
        	 var arrProvince = china[i][0].split(",");
            if(arrProvince[1] == selectProvince){
                var cityCount = china[i].length;
                var ddlCity = document.getElementById(cityId);
                ddlCity.innerHTML = "";
                for(var j=0; j<cityCount-1; j++){
                	var arrCity = china[i][j+1].split(",");
                	ddlCity.options[j] = new Option(arrCity[0],arrCity[1]);
                }
               
                break;
            }
        }
    }